package com.hzy.service;

import com.hzy.dao.FruitMapper;
import com.hzy.pojo.Fruit;
import com.hzy.utils.MybatisUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * @Package: com.hzy.service
 * @ClassName: FruitService
 * @Author: 862982795
 * @CreateTime: 2021/06/19 21:16
 * @Description:
 */
public class FruitService {
    SqlSession sqlSession = MybatisUtil.createSqlSession();//获取sqlSession对象
    List<Fruit> list;
    Integer i;//受影响的行数

    //分页查询所有信息
    public List<Fruit> getList(Integer pageBegin,Integer pageSize) {
        SqlSession sqlSession = MybatisUtil.createSqlSession();//获取sqlSession对象
        try {
            list = sqlSession.getMapper(FruitMapper.class).getList(pageBegin,pageSize);
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    //根据主键，查询信息
    public Fruit info(@Param("fruitId") Integer fruitId) {
        Fruit fruit;
        try {
            fruit = sqlSession.getMapper(FruitMapper.class).info(fruitId);
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return fruit;
    }

    // 获取总记录数
    public int getCount() {
        SqlSession sqlSession = MybatisUtil.createSqlSession();//获取sqlSession对象
        try {
            i = sqlSession.getMapper(FruitMapper.class).getCount();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return i;
    }

    //添加
    public int add(Fruit fruit) {
        try {
            i = sqlSession.getMapper(FruitMapper.class).add(fruit);
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return i;
    }

    //删除
    public int delete(Integer fruitId) {
        try {
            i = sqlSession.getMapper(FruitMapper.class).delete(fruitId);
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return i;
    }

    //修改
    public int update(Fruit fruit) {
        try {
            i = sqlSession.getMapper(FruitMapper.class).update(fruit);
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return i;
    }
}
